<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.0.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.0.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.0.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="一、消息中间件的应用场景异步处理场景：用户注册，信息写入数据库后，需要给用户发送注册成功的邮件，再发送注册成功的邮件。 1.同步调用：注册成功后，顺序执行发送邮件方法，发送短信方法，最后响应用户 2.并行调用：注册成功后，用多线程的方式并发执行发邮件和发短信方法，最后响应用户 3.消息队列：注册成功后，将要发送的消息用很短的时间写入消息队列中，之后响应用户；发送邮件的服务和发送短息的服务就可以从消">
<meta name="keywords" content="SpringBoot,rabbitmq">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot 整合 rabbitmq">
<meta property="og:url" content="http://yoursite.com/2019/03/08/SpringBoot-整合-rabbitmq/index.html">
<meta property="og:site_name" content="程序员果果的博客">
<meta property="og:description" content="一、消息中间件的应用场景异步处理场景：用户注册，信息写入数据库后，需要给用户发送注册成功的邮件，再发送注册成功的邮件。 1.同步调用：注册成功后，顺序执行发送邮件方法，发送短信方法，最后响应用户 2.并行调用：注册成功后，用多线程的方式并发执行发邮件和发短信方法，最后响应用户 3.消息队列：注册成功后，将要发送的消息用很短的时间写入消息队列中，之后响应用户；发送邮件的服务和发送短息的服务就可以从消">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee96822215?w=400&h=100&f=png&s=36319">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee973b51d7?w=300&h=150&f=png&s=39913">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee969f5331?w=440&h=190&f=png&s=51188">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee97c5224f?w=280&h=105&f=png&s=16954">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee9820ba77?w=290&h=195&f=png&s=22284">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee986b8c2b?w=410&h=100&f=png&s=35380">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec8b67de6?w=1240&h=702&f=png&s=95295">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec8ab3e12?w=1240&h=353&f=png&s=93192">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec97b31a8?w=1240&h=431&f=png&s=62854">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeece88307f?w=1240&h=305&f=png&s=147150">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeeca8f6a75?w=1240&h=345&f=png&s=182382">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeed1baac4a?w=1240&h=319&f=png&s=205656">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeef6365449?w=1240&h=809&f=png&s=128372">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eeef7a46e92?w=1240&h=719&f=png&s=174863">
<meta property="og:updated_time" content="2019-03-08T02:46:01.435Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SpringBoot 整合 rabbitmq">
<meta name="twitter:description" content="一、消息中间件的应用场景异步处理场景：用户注册，信息写入数据库后，需要给用户发送注册成功的邮件，再发送注册成功的邮件。 1.同步调用：注册成功后，顺序执行发送邮件方法，发送短信方法，最后响应用户 2.并行调用：注册成功后，用多线程的方式并发执行发邮件和发短信方法，最后响应用户 3.消息队列：注册成功后，将要发送的消息用很短的时间写入消息队列中，之后响应用户；发送邮件的服务和发送短息的服务就可以从消">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2019/2/28/16932eee96822215?w=400&h=100&f=png&s=36319">






  <link rel="canonical" href="http://yoursite.com/2019/03/08/SpringBoot-整合-rabbitmq/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>SpringBoot 整合 rabbitmq | 程序员果果的博客</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?48ba4ef106e9b2430083b8ec78a1831e";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">程序员果果的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/08/SpringBoot-整合-rabbitmq/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="程序员果果">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="程序员果果的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SpringBoot 整合 rabbitmq

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-03-08 10:42:19 / 修改时间：10:46:01" itemprop="dateCreated datePublished" datetime="2019-03-08T10:42:19+08:00">2019-03-08</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/SpringBoot/" itemprop="url" rel="index"><span itemprop="name">SpringBoot</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、消息中间件的应用场景"><a href="#一、消息中间件的应用场景" class="headerlink" title="一、消息中间件的应用场景"></a>一、消息中间件的应用场景</h2><h3 id="异步处理"><a href="#异步处理" class="headerlink" title="异步处理"></a>异步处理</h3><p>场景：用户注册，信息写入数据库后，需要给用户发送注册成功的邮件，再发送注册成功的邮件。</p>
<p>1.同步调用：注册成功后，顺序执行发送邮件方法，发送短信方法，最后响应用户<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee96822215?w=400&amp;h=100&amp;f=png&amp;s=36319" alt></p>
<p>2.并行调用：注册成功后，用多线程的方式并发执行发邮件和发短信方法，最后响应用户<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee973b51d7?w=300&amp;h=150&amp;f=png&amp;s=39913" alt></p>
<p>3.消息队列：注册成功后，将要发送的消息用很短的时间写入消息队列中，之后响应用户；发送邮件的服务和发送短息的服务就可以从消息队列中异步读去，然后发送任务。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee969f5331?w=440&amp;h=190&amp;f=png&amp;s=51188" alt></p>
<h3 id="应用解耦"><a href="#应用解耦" class="headerlink" title="应用解耦"></a>应用解耦</h3><p>场景：购物下单后，调用库存系统，更新库存。</p>
<p>1.耦合的方式：订单系统，写调用库存系统的逻辑。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee97c5224f?w=280&amp;h=105&amp;f=png&amp;s=16954" alt></p>
<p>2.解耦的方式：订单系统，将下达的消息写入消息队列，库存系统从消息队列中读取消息，更新库存。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee9820ba77?w=290&amp;h=195&amp;f=png&amp;s=22284" alt></p>
<h3 id="流量削峰"><a href="#流量削峰" class="headerlink" title="流量削峰"></a>流量削峰</h3><p>秒杀场景中，我们可以设置一个定长的消息队列，秒杀开始，谁快谁先进入队列，然后快速返回用户是否秒到 ，之后在平稳的处理秒杀后的业务。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eee986b8c2b?w=410&amp;h=100&amp;f=png&amp;s=35380" alt></p>
<h2 id="二、消息服务中间件概述"><a href="#二、消息服务中间件概述" class="headerlink" title="二、消息服务中间件概述"></a>二、消息服务中间件概述</h2><ul>
<li><ol>
<li>大多应用中，可通过消息服务中间件来提升系统异步通信、扩展解耦能力 </li>
</ol>
</li>
<li><ol start="2">
<li>消息服务中两个重要概念:<br>消息代理(message broker)和目的地(destination) 当消息发送者发送消息以后，将由消息代理接管，消息代理保证消息传递到指定目<br>的地。</li>
</ol>
</li>
<li><ol start="3">
<li>消息队列主要有两种形式的目的地</li>
</ol>
<ul>
<li><ol>
<li>队列(queue):点对点消息通信(point-to-point)</li>
</ol>
</li>
<li><ol start="2">
<li>主题(topic):发布(publish)/订阅(subscribe)消息通信</li>
</ol>
</li>
</ul>
</li>
<li><ol start="4">
<li>点对点式:</li>
</ol>
<ul>
<li><ol>
<li>消息发送者发送消息，消息代理将其放入一个队列中，消息接收者从队列中获取消息内容， 消息读取后被移出队列</li>
</ol>
</li>
<li><ol start="2">
<li>消息只有唯一的发送者和接受者，但并不是说只能有一个接收者 </li>
</ol>
</li>
</ul>
</li>
<li><ol start="5">
<li>发布订阅式:</li>
</ol>
<ul>
<li>发送者(发布者)发送消息到主题，多个接收者(订阅者)监听(订阅)这个主题，那么 就会在消息到达时同时收到消息</li>
</ul>
</li>
<li><ol start="6">
<li>JMS(Java Message Service)JAVA消息服务:</li>
</ol>
<ul>
<li>基于JVM消息代理的规范。ActiveMQ、HornetMQ是JMS实现</li>
</ul>
</li>
<li><ol start="7">
<li>AMQP(Advanced Message Queuing Protocol)</li>
</ol>
<ul>
<li>高级消息队列协议，也是一个消息代理的规范，兼容JMS</li>
<li>RabbitMQ是AMQP的实现</li>
</ul>
</li>
</ul>
<p><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec8b67de6?w=1240&amp;h=702&amp;f=png&amp;s=95295" alt></p>
<ul>
<li><ol start="8">
<li>Spring支持</li>
</ol>
<ul>
<li>spring-jms提供了对JMS的支持</li>
<li>spring-rabbit提供了对AMQP的支持</li>
<li>需要ConnectionFactory的实现来连接消息代理</li>
<li>提供JmsTemplate、RabbitTemplate来发送消息</li>
<li>@JmsListener(JMS)、@RabbitListener(AMQP)注解在方法上监听消息代理发 布的消息</li>
<li>@EnableJms、@EnableRabbit开启支持</li>
</ul>
</li>
<li><ol start="9">
<li>Spring Boot自动配置</li>
</ol>
<ul>
<li>JmsAutoConfiguration</li>
<li>RabbitAutoConfiguration</li>
</ul>
</li>
</ul>
<h2 id="三、RabbitMQ简介"><a href="#三、RabbitMQ简介" class="headerlink" title="三、RabbitMQ简介"></a>三、RabbitMQ简介</h2><p>RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue Protocol)的开源实现。</p>
<h3 id="1-核心概念"><a href="#1-核心概念" class="headerlink" title="1. 核心概念"></a>1. 核心概念</h3><ul>
<li><strong>Message</strong> ：消息，消息是不具名的，它由消息头和消息体组成。消息体是不透明的，而消息头则由一系列的可选属性组 成，这些属性包括routing-key(路由键)、priority(相对于其他消息的优先权)、delivery-mode(指出 该消息可能需要持久性存储)等。</li>
<li><strong>Publisher</strong> ：消息的生产者，也是一个向交换器发布消息的客户端应用程序。</li>
<li><strong>Exchange</strong> ：交换器，用来接收生产者发送的消息并将这些消息路由给服务器中的队列。Exchange有4种类型:direct(默认)，fanout, topic, 和headers，不同类型的Exchange转发消息的策略有所区别</li>
<li><strong>Queue</strong> ：消息队列，用来保存消息直到发送给消费者。它是消息的容器，也是消息的终点。一个消息可投入一个或多个队列。消息一直在队列里面，等待消费者连接到这个队列将其取走。</li>
<li><strong>Binding</strong> ：绑定，用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息队列连接起来的路由规则，所以可以将交换器理解成一个由绑定构成的路由表。Exchange 和Queue的绑定可以是多对多的关系。</li>
<li><strong>Connection</strong> ：网络连接，比如一个TCP连接。</li>
<li><strong>Channel</strong> ：信道，多路复用连接中的一条独立的双向数据流通道。信道是建立在真实的TCP连接内的虚 拟连接，AMQP 命令都是通过信道发出去的，不管是发布消息、订阅队列还是接收消息，这 些动作都是通过信道完成。因为对于操作系统来说建立和销毁 TCP 都是非常昂贵的开销，所 以引入了信道的概念，以复用一条 TCP 连接。</li>
<li><strong>Consumer</strong> ：消息的消费者，表示一个从消息队列中取得消息的客户端应用程序。</li>
<li><strong>Virtual Host</strong> ：虚拟主机，表示一批交换器、消息队列和相关对象。虚拟主机是共享相同的身份认证和加 密环境的独立服务器域。每个 vhost 本质上就是一个 mini 版的 RabbitMQ 服务器，拥有 自己的队列、交换器、绑定和权限机制。vhost 是 AMQP 概念的基础，必须在连接时指定， RabbitMQ 默认的 vhost 是 / 。</li>
<li><strong>Broker</strong> ：表示消息队列服务器实体。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec8ab3e12?w=1240&amp;h=353&amp;f=png&amp;s=93192" alt></li>
</ul>
<h2 id="四、RabbitMQ运行机制"><a href="#四、RabbitMQ运行机制" class="headerlink" title="四、RabbitMQ运行机制"></a>四、RabbitMQ运行机制</h2><h3 id="AMQP-中的消息路由"><a href="#AMQP-中的消息路由" class="headerlink" title="AMQP 中的消息路由"></a>AMQP 中的消息路由</h3><p>AMQP 中消息的路由过程和 Java 开发者熟悉的 JMS 存在一些差别，AMQP 中增加了 Exchange 和 Binding 的角色。生产者把消息发布到 Exchange 上，消息最终到达队列并被 消费者接收，而 Binding 决定交换器的消息应该发送到那个队列。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeec97b31a8?w=1240&amp;h=431&amp;f=png&amp;s=62854" alt></p>
<h3 id="Exchange-类型"><a href="#Exchange-类型" class="headerlink" title="Exchange 类型"></a>Exchange 类型</h3><p>Exchange分发消息时根据类型的不同分发策略有区别，目前共四种类型：direct、fanout、topic、headers 。headers 匹配 AMQP 消息的 header 而不是路由键， headers 交换器和 direct 交换器完全一致，但性能差很多，目前几乎用不到了，所以直接看另外三种类型：<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeece88307f?w=1240&amp;h=305&amp;f=png&amp;s=147150" alt></p>
<p><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeeca8f6a75?w=1240&amp;h=345&amp;f=png&amp;s=182382" alt></p>
<p><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeed1baac4a?w=1240&amp;h=319&amp;f=png&amp;s=205656" alt></p>
<h2 id="五、RabbitMQ安装"><a href="#五、RabbitMQ安装" class="headerlink" title="五、RabbitMQ安装"></a>五、RabbitMQ安装</h2><p>我们使用 docker 来安装 RabbitMQ。<br>我们在 docker hub上选择官方的带management管理界面的最新版本。<br><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeef6365449?w=1240&amp;h=809&amp;f=png&amp;s=128372" alt></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span>获取rabbitmq镜像</span><br><span class="line">docker pull rabbitmq:3-management</span><br><span class="line"><span class="meta">#</span>启动 rabbitmq镜像，5672是mq通信端口，15672是mq的web管理界面端口</span><br><span class="line">run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 镜像ID</span><br></pre></td></tr></table></figure>
<p>访问127.0.0.1:15672 ，用账号：guest 密码：guest 登录，界面如下：</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/2/28/16932eeef7a46e92?w=1240&amp;h=719&amp;f=png&amp;s=174863" alt></p>
<p>对rabbitmq的详细使用在这里，就不讲解了，我们这节的重点是整合rabbitmq。</p>
<h2 id="六、整合RabbitMQ"><a href="#六、整合RabbitMQ" class="headerlink" title="六、整合RabbitMQ"></a>六、整合RabbitMQ</h2><p>创建项目引入rabbitmq依赖。</p>
<h3 id="1-pom-xml"><a href="#1-pom-xml" class="headerlink" title="1. pom.xml"></a>1. pom.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.gf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springboot-rabbitmq<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">name</span>&gt;</span>springboot-rabbitmq<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">description</span>&gt;</span>Demo project for Spring Boot<span class="tag">&lt;/<span class="name">description</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.5.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">relativePath</span>/&gt;</span> <span class="comment">&lt;!-- lookup parent from repository --&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.reporting.outputEncoding</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">java.version</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">java.version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-amqp<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="2-MyAMQPConfig"><a href="#2-MyAMQPConfig" class="headerlink" title="2. MyAMQPConfig"></a>2. MyAMQPConfig</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.gf.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.support.converter.MessageConverter;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义消息转换器，默认是jdk的序列化转换器，我们自定义为json的</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAMQPConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> MessageConverter <span class="title">messageConverter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Jackson2JsonMessageConverter();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-springboot-测试类"><a href="#3-springboot-测试类" class="headerlink" title="3. springboot 测试类"></a>3. springboot 测试类</h3><p>我们测试创建管理配置、发送消息、接收消息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.gf;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.AmqpAdmin;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.Binding;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.DirectExchange;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.FanoutExchange;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.Queue;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.TopicExchange;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.rabbit.core.RabbitTemplate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringRunner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="meta">@RunWith</span>(SpringRunner.class)</span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootRabbitmqApplicationTests</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	RabbitTemplate rabbitTemplate;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	AmqpAdmin amqpAdmin;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextLoads</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">create</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="comment">//创建Exchange</span></span><br><span class="line">		amqpAdmin.declareExchange( <span class="keyword">new</span> DirectExchange( <span class="string">"exchange.direct"</span>) );</span><br><span class="line">		amqpAdmin.declareExchange( <span class="keyword">new</span> FanoutExchange( <span class="string">"exchange.fanout"</span>) );</span><br><span class="line">		amqpAdmin.declareExchange( <span class="keyword">new</span> TopicExchange( <span class="string">"exchange.topic"</span>) );</span><br><span class="line"></span><br><span class="line">		<span class="comment">//创建Queue</span></span><br><span class="line">		amqpAdmin.declareQueue( <span class="keyword">new</span> Queue( <span class="string">"direct.queue"</span> , <span class="keyword">true</span> ) );</span><br><span class="line">		amqpAdmin.declareQueue( <span class="keyword">new</span> Queue( <span class="string">"fanout.queue"</span> , <span class="keyword">true</span> ) );</span><br><span class="line"></span><br><span class="line">		<span class="comment">//绑定Queue</span></span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"direct.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.direct"</span> , <span class="string">"direct.queue"</span> , <span class="keyword">null</span> ) );</span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"fanout.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.direct"</span> , <span class="string">"fanout.queue"</span> , <span class="keyword">null</span> ) );</span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"direct.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.fanout"</span> , <span class="string">""</span> , <span class="keyword">null</span> ) );</span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"fanout.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.fanout"</span> , <span class="string">""</span> , <span class="keyword">null</span> ) );</span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"direct.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.topic"</span> , <span class="string">"direct.#"</span> , <span class="keyword">null</span> ) );</span><br><span class="line">		amqpAdmin.declareBinding( <span class="keyword">new</span> Binding( <span class="string">"fanout.queue"</span> , Binding.DestinationType.QUEUE , <span class="string">"exchange.topic"</span> , <span class="string">"direct.*"</span> , <span class="keyword">null</span> ) );</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send2Direct</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Map&lt;String , Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">		map.put( <span class="string">"msg"</span> , <span class="string">"这是一条点对点消息"</span> );</span><br><span class="line">		map.put( <span class="string">"data"</span> , Arrays.asList(<span class="string">"helloworld"</span> , <span class="number">123</span> , <span class="keyword">true</span>) );</span><br><span class="line"></span><br><span class="line">		rabbitTemplate.convertAndSend( <span class="string">"exchange.direct"</span> , <span class="string">"direct.queue"</span> , map );</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send2Topic</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Map&lt;String , Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">		map.put( <span class="string">"msg"</span> , <span class="string">"这是一条广播消息"</span> );</span><br><span class="line">		map.put( <span class="string">"data"</span> , Arrays.asList(<span class="string">"topic消息"</span> , <span class="number">123</span> , <span class="keyword">true</span>) );</span><br><span class="line"></span><br><span class="line">		rabbitTemplate.convertAndSend( <span class="string">"exchange.fanout"</span> , <span class="string">""</span>, map );</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receive</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Object o = rabbitTemplate.receiveAndConvert( <span class="string">"direct.queue"</span> );</span><br><span class="line">		o.getClass();</span><br><span class="line">		System.out.println(o.getClass());</span><br><span class="line">		System.out.println(o);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>监听消息</p>
<h3 id="4-启动类"><a href="#4-启动类" class="headerlink" title="4. 启动类"></a>4. 启动类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.gf;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.rabbit.annotation.EnableRabbit;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自动配置</span></span><br><span class="line"><span class="comment"> * 1. RabbitAutoConfiguration</span></span><br><span class="line"><span class="comment"> * 2. 自动配置了连接工厂ConnectionFactory</span></span><br><span class="line"><span class="comment"> * 3. RabbitProperties 封装了RabbitMQ的配置</span></span><br><span class="line"><span class="comment"> * 4. RabbitTemplate : 给RabbitMQ发送和接受消息</span></span><br><span class="line"><span class="comment"> * 5. AmqpAdmin : RabbitMQ系统管理功能组件</span></span><br><span class="line"><span class="comment"> * 6. <span class="doctag">@EnableRabbit</span> + <span class="doctag">@RabbitListener</span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@EnableRabbit</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootRabbitmqApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		SpringApplication.run(SpringbootRabbitmqApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="5-MQService"><a href="#5-MQService" class="headerlink" title="5. MQService"></a>5. MQService</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.gf.service;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.core.Message;</span><br><span class="line"><span class="keyword">import</span> org.springframework.amqp.rabbit.annotation.RabbitListener;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MQService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RabbitListener</span>(queues = <span class="string">"fanout.queue"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">receive</span><span class="params">(Message message)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"收到消息 : "</span> + <span class="keyword">new</span> String(message.getBody()));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>源码下载：<a href="https://github.com/gf-huanchupk/SpringBootLearning" target="_blank" rel="noopener">https://github.com/gf-huanchupk/SpringBootLearning</a></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SpringBoot/" rel="tag"># SpringBoot</a>
          
            <a href="/tags/rabbitmq/" rel="tag"># rabbitmq</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/08/SpringBoot-整合-elasticsearch/" rel="next" title="SpringBoot 整合 elasticsearch">
                <i class="fa fa-chevron-left"></i> SpringBoot 整合 elasticsearch
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/03/08/SpringBoot-整合-docker/" rel="prev" title="SpringBoot 整合 docker">
                SpringBoot 整合 docker <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="程序员果果">
            
              <p class="site-author-name" itemprop="name">程序员果果</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">17</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">22</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/gf-huanchupk" title="GitHub &rarr; https://github.com/gf-huanchupk" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:782969359@qq.com" title="E-Mail &rarr; mailto:782969359@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、消息中间件的应用场景"><span class="nav-number">1.</span> <span class="nav-text">一、消息中间件的应用场景</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#异步处理"><span class="nav-number">1.1.</span> <span class="nav-text">异步处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#应用解耦"><span class="nav-number">1.2.</span> <span class="nav-text">应用解耦</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#流量削峰"><span class="nav-number">1.3.</span> <span class="nav-text">流量削峰</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、消息服务中间件概述"><span class="nav-number">2.</span> <span class="nav-text">二、消息服务中间件概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、RabbitMQ简介"><span class="nav-number">3.</span> <span class="nav-text">三、RabbitMQ简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-核心概念"><span class="nav-number">3.1.</span> <span class="nav-text">1. 核心概念</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、RabbitMQ运行机制"><span class="nav-number">4.</span> <span class="nav-text">四、RabbitMQ运行机制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#AMQP-中的消息路由"><span class="nav-number">4.1.</span> <span class="nav-text">AMQP 中的消息路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Exchange-类型"><span class="nav-number">4.2.</span> <span class="nav-text">Exchange 类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#五、RabbitMQ安装"><span class="nav-number">5.</span> <span class="nav-text">五、RabbitMQ安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#六、整合RabbitMQ"><span class="nav-number">6.</span> <span class="nav-text">六、整合RabbitMQ</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-pom-xml"><span class="nav-number">6.1.</span> <span class="nav-text">1. pom.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-MyAMQPConfig"><span class="nav-number">6.2.</span> <span class="nav-text">2. MyAMQPConfig</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-springboot-测试类"><span class="nav-number">6.3.</span> <span class="nav-text">3. springboot 测试类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-启动类"><span class="nav-number">6.4.</span> <span class="nav-text">4. 启动类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-MQService"><span class="nav-number">6.5.</span> <span class="nav-text">5. MQService</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">程序员果果</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.0.1</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.1"></script>

  <script src="/js/src/motion.js?v=7.0.1"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.1"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.1"></script>




  
  <script src="/js/src/scrollspy.js?v=7.0.1"></script>
<script src="/js/src/post-details.js?v=7.0.1"></script>



  


  <script src="/js/src/next-boot.js?v=7.0.1"></script>


  

  


  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
